package com.aierkeji.call.b2c.dao.jpa.custom.ipml;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate5.HibernateTemplate;

import com.aierkeji.call.b2c.dao.jpa.custom.AkjCallClueHistoryDaoCustom;
import com.aierkeji.call.b2c.entity.AkjCallClueHistroyVo;


public class AkjCallClueHistoryDaoCustomImpl implements AkjCallClueHistoryDaoCustom{

	@Autowired
	HibernateTemplate hibernateTemplate;
	
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public List<AkjCallClueHistroyVo> findHistroyByCallClueId(String callClueId, String initDate, String finish) {
		
		String sql = "select t.call_at, t.call_time, t.id, t.clue_id, t.call_result, t.call_status, t.created_at, t.call_remark, t.phone, t.call_from from akj_call_clue_histroy t "
				+ "where t.clue_id = ?  "
				+ "ORDER BY t.call_at DESC LIMIT 0,3 ";
		
		List<Object> queryList = new ArrayList<Object>();
		queryList.add(callClueId);
		
		List<AkjCallClueHistroyVo> callClueHistroyList = jdbcTemplate.query(sql, new RowMapper<AkjCallClueHistroyVo>() {  
		    public AkjCallClueHistroyVo mapRow(ResultSet rs, int rowNum) throws SQLException {  
		    AkjCallClueHistroyVo akjCallClueHistroy = new AkjCallClueHistroyVo();  
		    akjCallClueHistroy.setId(rs.getLong("id"));
		    akjCallClueHistroy.setClueId(rs.getLong("clue_id"));
		    akjCallClueHistroy.setPhone(rs.getString("phone"));
		    akjCallClueHistroy.setCallTime(rs.getLong("call_time"));
		    akjCallClueHistroy.setCallStatus(rs.getString("call_status") == null ? "" : rs.getString("call_status"));
		    akjCallClueHistroy.setCallResult(rs.getString("call_result") == null ? "" : rs.getString("call_result"));
		    //akjCallClueVo.setLastCallStatus(CallStatus.valueOf(rs.getString("last_call_status")));
		    //akjCallClueVo.setLastCallResult(CallResult.valueOf(rs.getString("last_call_result")));
		    akjCallClueHistroy.setCallAt(rs.getTimestamp("call_at"));
		    akjCallClueHistroy.setCallFrom("call_from");
		    akjCallClueHistroy.setCallRemark(rs.getString("call_remark"));
		    akjCallClueHistroy.setCreatedAt(rs.getTimestamp("created_at"));
		    return akjCallClueHistroy;  
		    }  
		  },queryList.toArray());
		
		return callClueHistroyList;
	}
	
}
